package com.seari.tunnel.quartz;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.soap.SOAPException;

import org.springframework.beans.factory.annotation.Autowired;

import com.seari.tunnel.entity.AlarmHistory;
import com.seari.tunnel.entity.job.OPCData;
import com.seari.tunnel.service.IJOBService;
import com.seari.tunnel.service.IOperatMaintenanceService;

public class AlarmJob {
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	IJOBService jobService;
	@Autowired
	IOperatMaintenanceService operatMaintenanceService;
	
	public void listen() throws SOAPException, IOException, JAXBException{
		List<OPCData> dataSource = new ArrayList<OPCData>();
		jobService.browse("", dataSource);
		jobService.read(dataSource);
		for(int i=0;i<dataSource.size();i++){
			if("DEMO.level_al".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.level".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("液位过高");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
			if("DEMO.O2_al".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.O2".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("氧气浓度过低");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
			if("DEMO.O2_hal".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.O2".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("氧气浓度过高");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
			if("DEMO.temp1_al".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.temp1".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("温度过高");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
			if("DEMO.wet1_al".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.wet1".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("湿度过高");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
			if("DEMO.krqt_al".equals(dataSource.get(i).getOPCName())){
				if("true".equals(dataSource.get(i).getOPCValue())){
					for(int j=0;j<dataSource.size();j++){
    					if("DEMO.krqt".equals(dataSource.get(j).getOPCName())){
				                AlarmHistory ah = new AlarmHistory();
				                ah.setName("可燃气体浓度过高");
				                ah.setValue(dataSource.get(j).getOPCValue());
				                ah.setTime(sdf.format(new Date()));
				                operatMaintenanceService.addAlarmHistory(ah);
    					}
					}
				}
			}
		}
	}
}
